<template>
  <div id="UserRegisterPage">
    <div class="title"><h1>ai智能云图库</h1></div>
    <div class="desc">企业级智能协同云图库</div>

    <a-form :model="formState" autocomplete="off" name="basic" pla @finish="handleSubmit">
      <a-form-item :rules="[{ required: true, message: '请输入账号!' }]" name="userAccount">
        <a-input v-model:value="formState.userAccount" placeholder="请输入账号" />
      </a-form-item>

      <a-form-item
        :rules="[
          { required: true, message: '请输入密码!' },
          { min: 8, message: '密码不能小于8位!' },
        ]"
        name="userPassword"
      >
        <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" />
      </a-form-item>

      <a-form-item
        :rules="[
          { required: true, message: '请再次输入密码!' },
          { min: 8, message: '密码不能小于8位!' },
        ]"
        name="checkPassword"
      >
        <a-input-password v-model:value="formState.checkPassword" placeholder="请再次输入密码" />
      </a-form-item>

      <div class="tips">
        已有账号：
        <RouterLink to="/user/login">去登录</RouterLink>
      </div>

      <a-form-item>
        <a-button html-type="submit" style="width: 100%" type="primary">注册</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import UserRegisterRequest = API.UserRegisterRequest
import { userRegisterUsingPost } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import router from '@/router'

//表单内容
const formState = reactive<UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
  remember: true,
})

//表单提交
const handleSubmit = async (values: any) => {
  const res=await userRegisterUsingPost(values)
  if(values.userPassword!=values.checkPassword){
    message.error("两次输入的密码不一致")
    return;
  }
  if(res.data.code==0 && res.data.data){
    message.success("注册成功")
    await router.push({
      path: "/user/login",
      replace: true
    })
  }else {
    message.error("注册失败:"+res.data.message)
  }
}
</script>

<style scoped>
#UserRegisterPage {
  max-width: 360px;
  margin: 0 auto;
}

.title {
  text-align: center;
  margin-bottom: 16px;
}

.desc {
  text-align: center;
  color: #bbb;
  margin-bottom: 16px;
}
.tips{
  text-align: right;
  margin-bottom: 14px;
}
</style>
